﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;
using CNVP.Config;

namespace CNVP.JShop.Web.Admin
{
    public partial class UserEdit : AdminPage
    {
        BLL.CNVP_Users bll = new BLL.CNVP_Users();
        Model.CNVP_Users model = new Model.CNVP_Users();
        public int id = 0;
        public int seoInId = 0;
        public string Province = "0", City = "0", Area = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 90)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ProvinceBind();
                ddlNewsClass();
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                    Pwd1.Visible = false;
                    Pwd2.Visible = false;
                    txtUserName.CssClass = "Display";
                    
                }
                else
                {
                    int UserLevelID = 0;
                    if (!string.IsNullOrEmpty(Request.QueryString["UserLevelID"]) && Public.IsNumber(Request.QueryString["UserLevelID"]))
                    {
                        UserLevelID = Convert.ToInt32(Request.QueryString["UserLevelID"]);
                    }
                    if (UserLevelID > 0)
                    {
                        ddlUserLevelID.SelectedValue = UserLevelID.ToString();
                    }
                    AddIP.Visible = false;
                    AddTime.Visible = false;
                    EndLoginTime.Visible = false;
                    EndLoginIP.Visible = false;
                    LoginNum.Visible = false;
                    lbTitle.Text = " > 添加";
                    lbtxtUserName.Text = "注：勿使用"+UIConfig.UserFilter;
                    txtUserName.Attributes.Add("onchange", "IsUserName()");
                }
                ddlProvince.Attributes.Add("onchange", "ProvinceChange()");
            }
            
        }

        private void ProvinceBind()
        {
            IList<Model.CNVP_Province> model = new BLL.CNVP_Province().GetAll_CNVP_Province();
            foreach (Model.CNVP_Province p in model)
            {
                ddlProvince.Items.Add(new ListItem(p.Name, p.Code));
            }
        }

        private void ddlNewsClass()
        {
            IList<Model.CNVP_UserLevel> model = new BLL.CNVP_UserLevel().GetAll_CNVP_UserLevel("where IsDelete=0");
            foreach (Model.CNVP_UserLevel c in model)
            {
                ListItem lt = new ListItem(c.LevelName, c.ID.ToString());
                ddlUserLevelID.Items.Add(lt);
                if (c.IsAcquiesce == 1)
                {
                    ddlUserLevelID.SelectedValue = c.ID.ToString();
                }
            }
        }
        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_UsersById(id);
            if (model != null)
            {
                lbtxtUserName.Text = model.UserName;
                txtUserName.Text = model.UserName;
                ddlUserLevelID.SelectedValue = model.UserLevelID.ToString();
                txtEmail.Text = model.Email;
                txtOtherText.Text = model.OtherText;
                lbAddTime.Text = model.AddTime.ToString();
                lbAddIP.Text = model.AddIP;
                lbEndLoginTime.Text = model.EndLoginTime.ToString();
                lbEndLoginIP.Text = model.EndLoginIP;
                lbLoginNum.Text = model.LoginNum.ToString();
                txtMobilePhone.Text = model.MobilePhone;
                ddlProvince.SelectedValue = model.Province;
                Province = string.IsNullOrEmpty(model.Province) ? "0" : model.Province;
                City = string.IsNullOrEmpty(model.City) ? "0" : model.City;
                Area = string.IsNullOrEmpty(model.Area) ? "0" : model.Area;
                txtGetAddress.Text = model.GetAddress;
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            string UserName = txtUserName.Text.Trim();
            if (UserName == "")
            {
                MessageUtils.ShowPre("请输入会员账户！");
                return;
            }
            string Email = txtEmail.Text.Trim();
            if (id > 0)
            {
                int rows = new BLL.CNVP_Users().Update_CNVP_Users(
                       new Model.CNVP_Users()
                       {
                           ID = id,
                           UserName = txtUserName.Text.Trim(),
                           UserLevelID = int.Parse(ddlUserLevelID.SelectedValue),
                           Email = txtEmail.Text.Trim(),
                           EndLoginTime = DateTime.Now.AddSeconds(1),
                           EndLoginIP = HttpContext.Current.Request.UserHostAddress,
                           OtherText = txtOtherText.Text.Trim(),
                           Province = ddlProvince.SelectedValue,
                           City = Request.Form["ListCity"],
                            Area = Request.Form["ListArea"],
                            GetAddress = txtGetAddress.Text,
                            MobilePhone = txtMobilePhone.Text
                       }

                );
                if (rows == 1)
                {
                    BasePage.MessageBox("修改成功！", "UserList.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                { BasePage.MessageBox("修改失败！"); }
            }
            else
            {
                
                string _UserFilter = UIConfig.UserFilter;
                if (_UserFilter != "")
                {
                    string[] UserFilterArray = _UserFilter.Split(Convert.ToChar("|"));
                    for (int i = 0; i < UserFilterArray.Length; i++)
                    {
                        if (UserFilterArray[i].ToLower() == UserName.ToLower())
                        {
                            MessageUtils.ShowPre("会员账户不允许使用" + _UserFilter + "！");
                            return;
                        }
                    }
                }
                var UsersList = new BLL.CNVP_Users().QurryListByUserName(this.txtUserName.Text);
                if (UsersList.Count > 0)
                {
                    BasePage.MessageBox("会员账户已经存在，请重新输入！");
                    return;
                }
                else
                {
                    int rows = new BLL.CNVP_Users().Add_CNVP_Users(new Model.CNVP_Users()
                    {
                        UserName = txtUserName.Text.Trim(),
                        UserPass = EncryptUtils.MD5(txtPwd1.Text.Trim()),
                        UserLevelID = int.Parse(ddlUserLevelID.SelectedValue),
                        Email = txtEmail.Text.Trim(),
                        PointsNum = 0,
                        TotalAmount = 0,
                        AddTime = DateTime.Now,
                        AddIP = HttpContext.Current.Request.UserHostAddress,
                        EndLoginTime = DateTime.Now,
                        EndLoginIP = HttpContext.Current.Request.UserHostAddress,
                        OtherText = txtOtherText.Text.Trim(),
                        IsDelete = 0,
                        LoginNum = 0,
                        Province = ddlProvince.SelectedValue,
                        City = Request.Form["ListCity"],
                        Area = Request.Form["ListArea"],
                        GetAddress = txtGetAddress.Text,
                        MobilePhone = txtMobilePhone.Text
                    });
                    if (rows == 1)
                    {
                        BasePage.MessageBox("添加成功！", "UserList.aspx");

                    }
                    else
                    { BasePage.MessageBox("添加失败！"); }
                }
            }
        }
    }
}